<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
class CatesController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    public static function getcates(){
        // 跳转类别和添加分隔符
        //跳转类别顺序连贯方法 Raw 原始表达式，反之sql语句注入
        $cates = DB::table("cates")->select(DB::raw("*,concat(path,',',id)as paths"))->orderBy("paths")->get();
        foreach($cates as $key => $value){
            $path = $value->path;

            // 转换为数组
            $arr = explode(",",$path);

            $len = count($arr)-1;

            $cates[$key]->name = str_repeat("--|",$len).$value->name;
        }
        return $cates;
    }

    public function index(Request $request)
    {
        //
        // 获取搜索关键词
        $k = $request->input('keyword');
        //列表
        //调整类别顺序，连贯方法，select 原始表达式，防止sql语句注入
        // concat(),拼接字符串
        $cates = DB::table("cates")->select(DB::raw("*,concat(path,',',id)as paths"))->where("name","like","%".$k."%")->orderBy("paths")->get();

        foreach($cates as $key => $value){
            $path = $value->path;
            //转换为数组，多少个逗号就遍历多少个--|
            $arr = explode(",",$path);

            // 获取逗号个数
            $len=count($arr)-1;
            $cates[$key]->name=str_repeat("--|",$len).$value->name;
            // str_repeat('要重复的内容',重复次数);
        }
        //跳转并传参数
        return view("Admin.Cates.index",['cates'=>$cates,'request'=>$request->all()]);
        
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
       $cates = self::getcates();
        return view("Admin.Cates.add",['cates'=>$cates]);

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
       // 排除_token
       $data = $request->except('_token');

       // 获取pid
       $pid = $request->input("pid");

       // 区分两种添加
       if($pid==0){
        $data['path']='0';
       }else{
        $info = DB::table("cates")->where("id","=",$pid)->first();
        $data['path']=$info->path.",".$pid;
       }

       if(DB::table('cates')->insert($data)){
            return redirect("/admincates")->with("success","添加成功");
       }else{
        return back()->with("error","添加失败");
       }
    }



    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $s = DB::table("cates")->where("pid","=",$id)->count();
      
        if($s>0){

            return redirect("/admincates")->with("error","请先干掉孩子");
        }else{
            // 直接删除类别
            if(DB::table("cates")->where("id","=",$id)->delete()){
                return redirect("/admincates")->with("success","删除成功");
            }else{
                return redirect("/admincates")->with("error","删除失败");
            }
        }

    }
}
